package apigateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class CorsConfig {
    //统一跨域配置：只要创建自定义的跨域过滤器对象注入到容器中，spring就会按照我们自己的规则处理跨域请求
    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        //配置自定义的跨域请求的参数
        CorsConfiguration config = new CorsConfiguration();
        //CorsConfiguration 相关配置说明
        // 是否允许携带cookies
        //private Boolean allowCredentials;
        // 允许的请求源
        // private List<String> allowedOrigins;
        // 允许的http方法
        //private List<String> allowedMethods;
        // 允许的请求头
        //private List<String> allowedHeaders;
        config.addAllowedOrigin("*");
        config.addAllowedMethod("*");
        config.addAllowedHeader("*");
        config.setAllowCredentials(true);
        source.registerCorsConfiguration("/**" , config);


        CorsWebFilter webFilter = new CorsWebFilter(source);

        return webFilter;
    }



}
